package com.wf.smartcomm.server.weixin.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;


import com.wf.smartcomm.server.client.action.BaseAction;
import com.wf.smartcomm.server.util.SignUtil;
import com.wf.smartcomm.server.weixin.service.WechatService;

@Service
@Scope("prototype")
public class WechatCallAction extends BaseAction {
	
	@Resource
	private WechatService wechatService;
	
	public void wechatCall() throws IOException{
		String method = request.getMethod();
		logger.info("weixin callback ---> method:"+method);
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		if (method.equals("GET")) {
			if (SignUtil.checkSignature("haochikeji", signature,
					timestamp, nonce)) {
				try {
					response.getWriter().print(echostr);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			String respMessage = wechatService.coreService(request);
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/xml;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(respMessage);
			out.close();
		}
	}
}
